{extend name="property/public/insidePageBase" /}
{block name="title"}维修工单列表{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                </div>
            </script>
            <table id="menu-table" class="layui-table" lay-filter="menu-table"></table>
        </div>
    </div>
</div>
<script type="text/html" id="auth-state">
    <div class="layui-btn-group">
        {if $editAuth} <a class="layui-btn layui-btn-sm  layui-btn-normal" lay-event="edit">编辑</a>{/if}
        {if $detailAuth}<a class="layui-btn layui-btn-sm" lay-event="detail">详情</a>{/if}
        {if $delAuth} <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>{/if}
    </div>

</script>
<script type="text/html" id="picture_tpl">
    <img src="{{ d.picture }}" style="height:100%" class="checkPictureByImg" />
</script>
<script type="text/html" id="is_show_tpl">
    <input type="checkbox" lay-skin="switch" value="{{ d.id }}" disabled  lay-filter="isShow" {{# if(d.is_show == 1) { }}checked{{# } }} lay-text="显示|隐藏" />
</script>
<script type="text/html" id="time_type_tpl">
    {{# if(d.time_type === 1) { }}<span class="layui-badge layui-bg-blue">紧急不重要</span>{{# } }}
    {{# if(d.time_type === 2) { }}<span class="layui-badge layui-bg-orange">重要不紧急</span>{{# } }}
    {{# if(d.time_type === 3) { }}<span class="layui-badge layui-bg-reg">重要且紧急</span>{{# } }}
</script>
<script type="text/html" id="time_limit_tpl">
    {{# if(d.time_limit > 0) { }}
        <span class="layui-badge layui-bg-green"> {{d.time_limit}} 小时内完成</span>
    {{# }else{ }}

    {{# } }}
</script>
{/block}
{block name="js"}
{__block__}
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        var tableId = 'menu-table';
        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#'+tableId,
            toolbar: '#toolbar',
            id:tableId,
            url: '{:url("propertyRepairCateList")}',
            page: false,
            treeDefaultClose:true,
            cols: [[
                {type: 'numbers'},
                {field: 'cate_name', width: 200, title: '分类名称'},
                {field: 'picture', width: 80, title: '图标',templet:'#picture_tpl'},
                {field: 'is_show', width: 100, title: '状态', templet:'#is_show_tpl', sort: true},
                {field: 'time_type', width: 128, title: '工单属性', templet:'#time_type_tpl', sort: true},
                {field: 'time_limit', width: 128, title: '维修限时', templet:'#time_limit_tpl', sort: true},
                {field: 'amount', width: 108, title: '工单价格', templet:function(d) {
                        return d.amount + ' ¥';
                    },sort: true},
                {field: 'add_time', width: 160, title: '创建时间', sort: true},
                {templet: '#auth-state', minWidth: 168,  title: '操作', fixed:'right'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
        $('#btn-expand').click(function () {
            treetable.expandAll('#'+tableId);
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#'+tableId);
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar('+tableId+')', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var url = '{:url("propertyRepairCateAdd")}';
                var index = layer.open({ title: '添加工单分类', type: 2, shade: 0.2, maxmin:true, area: ['650px', '460px'], content: url  , btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index] , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg, {icon: 6, offset: '15px'});
                                    location.reload();
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听工具条
        table.on('tool('+tableId+')', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                var url = '{:url("propertyRepairCateEdit")}?id=' + data.id;
                var index = layer.open({ title: '编辑工单分类',type: 2, shade: 0.2, maxmin:true, area: ['650px', '460px'], content: url , btnAlign: 'c' , btn: ['确定', '取消'] , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    layer.close(index); //关闭弹层
                                    location.reload();
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                    , success: function (layero, index) {

                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            if (layEvent === 'delete') {
                var url = '{:url("propertyRepairCateDel")}?id=' + data.id;
                layer.confirm('删除后不可恢复！是否确定？',{icon:3}, function(){
                    var loadAdd = layer.msg('提交中',{icon:16, time:0, shade: 0.1, offset:"15px"})
                    $.post(url, {id:data.id}, function(res){
                        layer.close(loadAdd)
                        if(res.code === 0 ){
                            location.reload();
                            layer.msg(res.msg,{icon:6, offset: '15px'})
                        }else {
                            layer.msg(res.msg, {icon:5, offset: '15px'})
                        }
                    })
                })
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }else if(obj.event === 'detail'){
                var url = '{:url("propertyRepairCateDetail")}?id='+data.id;
                var index = parent.layer.open({
                    title: data.cate_name +'  详情', type: 2, shade: 0.2, maxmin:true, shadeClose:true, move:false,area: ['1000px', '100%'],offset:  'rt', anim: 2, content: url , btn:false
                });
                return false;
            }
        });
    });
</script>
{/block}